 /**
 * Description: 物资管理
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.report.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.excel.JxlsView;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.service.inf.IBizMaterialsManageService;
import com.metro.paperless.common.beans.constant.DicTypeEnum;
import com.metro.paperless.common.beans.constant.JxlsTemplateEnum;
import com.metro.paperless.common.utils.DicUtil;
import com.metro.paperless.report.beans.vo.ReportOutAndIn;
import com.metro.paperless.report.beans.vo.ReportStock;
import com.metro.paperless.report.service.inf.IReportStockService;
import com.metro.paperless.sys.beans.vo.SysMaterialsType;
import com.metro.paperless.sys.service.inf.ISysMaterialsTypeService;
import com.metro.paperless.web.controller.BaseController;
 /**
 * 物资管理<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Controller
@RequestMapping("/reportStock")
public class ReportStockController extends BaseController {
    //菜单编码
    private static final String menuCode = "REPORT_STOCK";
    //模块描述
    private static final String MODULE_DESC = "库存明细";
    @Autowired
    private IReportStockService reportStockService;
    @Autowired
    private ISysMaterialsTypeService materialsTypeService;
    @Override
    protected String getViewDir() {
        return "report/";
    }
    
    /**
     * 去查询物资管理
     * 
     * @param bizmaterialsmanage 物资管理页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资管理列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(ReportStock reportStock) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("reportStock_list"));
            mav.addObject("materialsTypes", DicUtil.getLabelValueList(DicTypeEnum.MAT_TYPE));
            mav.addObject("searchEntity", reportStock);
        } catch (Exception ex) {
            log.error("查询物资管理出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询物资管理
     *
     * @param bizmaterialsmanage 物资管理页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资管理列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(ReportStock reportStock, Page page, QueryParams<ReportStock> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(reportStock);
            PageInfo<ReportStock> pageInfo = reportStockService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询物资管理出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 物资列表
     * @return
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查看物资借还记录")
    @RequestMapping(value="select", method = {RequestMethod.GET})
    public ModelAndView createSelectForm(){
    	ModelAndView mav= new ModelAndView();
    	List<SysMaterialsType> materialsTypeList = materialsTypeService.queryForList(new SysMaterialsType());
    	try{
    		mav.setViewName(getView("appmaterials_select"));
    		mav.addObject("materialsTypeList", materialsTypeList);
    	}catch(Exception ex){
    		log.error("去新增物资管理出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
    	}
    	return mav;
    }
    
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资管理列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolistAll", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolistAll(ReportStock reportStock) {
        JsonResult jr = new JsonResult();
        try {
            List<ReportStock> list = reportStockService.queryForList(reportStock);
            jr.setData(list);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询物资管理出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    
    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public ModelAndView export(ReportStock entity) {
    	
        List<ReportStock> list = reportStockService.queryForList(entity);
        return new ModelAndView(new JxlsView(JxlsTemplateEnum.STOCK, list));
    }
    
}
